﻿using B.ERP.Services;
using B.ERP.Services.Input;
using B.ERP.Services.OutPut;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Standard;

namespace B.ERP.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MaterialController : ControllerBase
    {
        private readonly IBOMServices _erpService;
        public MaterialController(IBOMServices mesService)
        {
            _erpService = mesService;
        }
        private static int serialNumber = 1; // 静态变量，用于记录流水号，初始为1
        /// <summary>
        /// 编码（流水号）
        /// </summary>
        /// <param name="codeType"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public string GetCode()
        {
            var result = new ApiResult<string>();
            //获取表中的数量，在该数量上加1
            var count = _erpService.GetMaterialcount().ResultData.Count;
            var serialNumber = count + 1;
            string serialPart = serialNumber.ToString("D7");
            string code = "ERP-WL" + DateTime.Now.ToString("yyyyMMddHH") + serialPart;
            serialNumber++;
            return code;
        }
        [HttpPost]
        public ApiResult<int> Add(AddMaterialInput input)
        {
            input.MaterialCode = GetCode();
            return _erpService.AddMaterial(input);
        }
        [HttpDelete]
        public ApiResult<int> Delete(string Id)
        {
            return _erpService.MaterialDeleteList(Id.Split(',').ToList());
        }
        /// <summary>
        /// 物料列表查询分页
        /// </summary>
        /// <param name="searchMaterialInput"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiPaging<GetMaterialOutPut> GetMaterial([FromQuery] SearchMaterialInput searchMaterialInput)
        {
            return _erpService.GetMaterial(searchMaterialInput);
        }
        /// <summary>
        /// 物料修改
        /// </summary>
        /// <param name="editMaterialInput"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> UpdateMaterial(EditMaterialInput editMaterialInput)
        {
            return _erpService.UpdateMaterial(editMaterialInput);
        }

    }
}
